package com.lw.extraceim.zxing;

import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Log;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.integration.android.IntentIntegrator;

import java.util.Hashtable;


public class QRCodeUtil {
    private static final String TAG = "QRCodeUtil";

    public static Bitmap createQrCode(String url, int width, int height) throws WriterException {
        Log.d(TAG, "createQrCode: " + url);

        Hashtable<EncodeHintType,String> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bm = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE,width,height,hints);

        int[] pixels = new int[width * height];
        //按照二维码的算法，逐个生成二位码的图片
        for(int y = 0;y < height;y++){
            for(int x = 0;x < width;x++){
                if(bm.get(x,y)){
                    pixels[y*width + x] = 0xff000000;
                }else{
                    pixels[y * width + x] = 0xffffffff;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels,0,width,0,0,width,height);
        return bitmap;
    }

}

